package uk.ac.durham.tel.synergynet.covanto.project.ui.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import uk.ac.durham.tel.commons.jface.node.AbstractBaseParent;
import uk.ac.durham.tel.commons.jface.node.DisposeException;
import uk.ac.durham.tel.commons.jface.node.IParentNode;
import uk.ac.durham.tel.synergynet.covanto.project.model.ProjectInfo;

public class ProjectNode extends AbstractBaseParent<ProjectInfo> {
	
	public ProjectNode(ProjectInfo resourceValue, IParentNode parentValue) {
		super(resourceValue, parentValue);
		if (resource.isProjectResourceOpened())
			this.setLabel(resource.getProjectResource().getName() + " (active)");
		else this.setLabel(resource.getProjectResource().getName());
		addProjectInfoPropertyChangeListener();
	}
	
	private PropertyChangeListener projectResourceOpenedChangeListener = new PropertyChangeListener() {
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			updateProjectNodeIcon();
		}
	};

	private void addProjectInfoPropertyChangeListener() {
		this.resource.addPropertyChangeListener(ProjectInfo.PROP_IS_PROJECT_RESOURCE_OPENED, projectResourceOpenedChangeListener);
	}

	private void updateProjectNodeIcon() {
		if (resource.isProjectResourceOpened())
			this.setLabel(resource.getProjectResource().getName() + " (active)");
		else this.setLabel(resource.getProjectResource().getName());
	}

	@Override
	public List<String> getChildrenNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void dispose() throws DisposeException {
		removeProjectInfoPropertyChangeListener();
	}

	private void removeProjectInfoPropertyChangeListener() {
		this.resource.addPropertyChangeListener(projectResourceOpenedChangeListener);
	}

	@Override
	public ImageDescriptor getIcon() {
		if (resource.isProjectResourceOpened())
			return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT);
		else 
			return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED);
	}

}
